Conditions | 24 |
Paths | 844 |
Total Lines | 133 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like stringFuncs.decodeCSV often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** global: UB */ |
||
95 | var stringFuncs = { |
||
96 | |||
97 | /** Decodes the given CSV file string and returns the cell data as an array. |
||
98 | * Extremely robust and fast CSV parser. Only parser that works with all bizarre but valid test files. |
||
99 | * |
||
100 | * 3 modes are available: |
||
101 | * if `headers` is null - All cells are returned as 2D array. (default) |
||
102 | * if `headers` is given and 0 length - The first row is stored in `headers`, remaining cells and returned as 2D array. |
||
103 | * if `headers` is given and >0 length - All rows are returned as objects, with the given headers treated as the prop names for the objects. |
||
104 | */ |
||
105 | decodeCSV: function (headers, trimValues, columnar = false, seperator = "auto") { |
||
106 | var csvString = this.toString(); |
||
107 | |||
108 | // cut String into lines |
||
109 | var lines = csvString.trim().splitLines(); |
||
110 | var sep = seperator == "auto" ? UB.CSV_detectSeperator(csvString, lines.length) : seperator; |
||
111 | |||
112 | // config |
||
113 | var hasHeaders = headers != null; |
||
114 | var returnAsObjs = hasHeaders && headers.exists(); |
||
115 | |||
116 | // status |
||
117 | var inQuoted = false; |
||
118 | |||
119 | // result |
||
120 | var linesData = []; |
||
121 | var word = []; |
||
122 | var tempHeaders = []; |
||
123 | var lineWords = []; |
||
124 | |||
125 | // per line |
||
126 | for (var l = 0, ll = lines.length; l < ll; l++) { |
||
127 | var line = lines[l]; |
||
128 | var isHeader = (l === 0 && hasHeaders); |
||
129 | |||
130 | // if we are in quoted text |
||
131 | if (inQuoted) { |
||
132 | |||
133 | // keep taking chars |
||
134 | |||
135 | }else{ |
||
136 | |||
137 | // save words into headers array / new array |
||
138 | lineWords = []; |
||
139 | if (isHeader){ |
||
140 | if (returnAsObjs){ |
||
141 | lineWords = tempHeaders; |
||
142 | }else{ |
||
143 | lineWords = headers; |
||
144 | } |
||
145 | } |
||
146 | if (!isHeader) { |
||
147 | linesData.push(lineWords); |
||
148 | } |
||
149 | |||
150 | } |
||
151 | |||
152 | // per char |
||
153 | for (var c = 0, clast = line.length - 1; c <= clast; c++) { |
||
154 | var ch = line.charAt(c); |
||
155 | |||
156 | // if we are in quoted text |
||
157 | if (inQuoted) { |
||
158 | |||
159 | // quotes.. |
||
160 | if (ch == "\"") { |
||
161 | |||
162 | // quote may be escaped |
||
163 | if (line.charAt(c + 1) == "\"") { |
||
164 | c++; |
||
165 | word.push("\""); |
||
166 | }else { |
||
167 | |||
168 | // quote means ending quoted text |
||
169 | inQuoted = false; |
||
170 | } |
||
171 | |||
172 | continue; |
||
173 | } |
||
174 | |||
175 | // normal char |
||
176 | word.push(ch); |
||
177 | |||
178 | |||
179 | }else { |
||
180 | |||
181 | // quote means beginning quoted text |
||
182 | if (ch == "\""){ |
||
183 | inQuoted = true; |
||
184 | continue; |
||
185 | } |
||
186 | |||
187 | // comma means end of word |
||
188 | if (ch == sep) { |
||
189 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
190 | word = []; |
||
191 | continue; |
||
192 | } |
||
193 | |||
194 | // normal char |
||
195 | word.push(ch); |
||
196 | |||
197 | // newline means end of word |
||
198 | if (c == clast) { |
||
199 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
200 | word = []; |
||
201 | } |
||
202 | |||
203 | } |
||
204 | |||
205 | } |
||
206 | |||
207 | // at end of line take word |
||
208 | if (!inQuoted && word.Length > 0) { |
||
209 | lineWords.push(trimValues ? word.join("").trim() : word.join("")); |
||
210 | word = []; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | // convert array to objs |
||
215 | if (returnAsObjs){ |
||
216 | |||
217 | // go thru all rows |
||
218 | for (var l = 0, ll = linesData.length; l < ll; l++) { |
||
219 | var row = linesData[l]; |
||
220 | var obj = {}; |
||
221 | |||
222 | // convert all cells to obj props |
||
223 | for (var h = 0, hl = headers.length; h < hl; h++) { |
||
224 | var header = headers[h]; |
||
225 | obj[header] = row[h]; |
||
226 | } |
||
227 | linesData[l] = obj; |
||
228 | } |
||
383 |